#include "data_global.h"

#include "buzzer.h"
#include "linuxuart.h"

extern int msgid;
extern key_t key;

extern pthread_mutex_t mutex_client_request,
	   mutex_refresh,
	   mutex_sqlite,
	   mutex_transfer,
	   mutex_analysis,
	   mutex_sms,
	   mutex_buzzer,
	   mutex_led,
	   mutex_camera;

extern pthread_cond_t  cond_client_request,
	   cond_refresh,
	   cond_sqlite,
	   cond_transfer,
	   cond_analysis,
	   cond_sms,
	   cond_buzzer,
	   cond_led,
	   cond_camera;

extern unsigned char cmd_led;
extern unsigned char cmd_buzzer;
extern unsigned char cmd_seg;
extern unsigned char cmd_fan;


extern char recive_phone[12] ;
extern char center_phone[12] ;


struct msg msgbuf;


//��2N��Ԫ�ر�ʾ����   ��2N+1��Ԫ�ر�ʾ��������ʱ��
unsigned char MUSIC[500] ={
	//ף��ƽ��
	0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
	0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
	0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
	0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
	0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
	0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
	0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
	0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
	0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
	0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
	0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
	0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
	0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
	0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,
#if 0
	0x30,0x1c,0x10,0x20,0x40,0x1c,0x10,0x18,0x10,0x20,0x10,0x1c,0x10,0x18,0x40,0x1c,
	0x20,0x20,0x20,0x1c,0x20,0x18,0x20,0x20,0x80,0xff,0x20,0x30,0x1c,0x10,0x18,0x20,
	0x15,0x20,0x1c,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2b,0x20,0x26,0x20,0x20,0x20,
	0x30,0x80,0xff,0x20,0x20,0x1c,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2b,0x20,0x30,
	0x20,0x2b,0x40,0x20,0x20,0x1c,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2b,0x20,0x30,
	0x20,0x2b,0x40,0x20,0x30,0x1c,0x10,0x18,0x20,0x15,0x20,0x1c,0x20,0x20,0x20,0x26,
	0x40,0x20,0x20,0x2b,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1c,0x10,0x20,
	0x10,0x1c,0x10,0x20,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,0x20,0x15,0x1f,
	0x05,0x20,0x10,0x1c,0x10,0x20,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,0x20,
	0x30,0x1c,0x10,0x18,0x20,0x15,0x20,0x1c,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2b,
	0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1c,0x10,0x18,0x40,0x1c,0x20,0x20,
	0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00,
#endif
};


void *pthread_client_request(void *arg)
{
	if((key = ftok("/tmp",'g')) < 0){
		perror("ftok failed .\n");
		exit(-1);
	}

	msgid = msgget(key,IPC_CREAT|IPC_EXCL|0666);
	if(msgid == -1)	{
		if(errno == EEXIST){
			msgid = msgget(key,0777);
		}else{
			perror("fail to msgget");
			exit(1);
		}
	}
	printf("pthread_client_request\n");

	while(1){
		bzero(&msgbuf,sizeof(msgbuf));
		printf("wait form client request...\n");
		msgrcv (msgid, &msgbuf, sizeof (msgbuf) - sizeof (long), 1L, 0);
		printf ("Get %ldL msg\n", msgbuf.msgtype);
		printf ("text[0] = %#x\n", msgbuf.text[0]);

		switch(msgbuf.msgtype){
			case 1L:
				pthread_mutex_lock(&mutex_led);
				printf("hello led\n");
				cmd_led = msgbuf.text[0];
				pthread_mutex_unlock(&mutex_led);
				pthread_cond_signal(&cond_led);  //signal to pthread_led
				break;
			case 2L:
				pthread_mutex_lock(&mutex_buzzer);
				printf("hello beep\n");
				cmd_buzzer = msgbuf.text[0];
				printf("cmd_buzzer :%#x.\n",cmd_buzzer);
			
				int i = 0;
				int buzzer_fd;
				beep_desc_t beeper;
				buzzer_fd = open(BEEPER_DEV,O_RDWR | O_NONBLOCK);
				if ( buzzer_fd == -1 ) {
					perror("open beeper failed.\n");
					//return -1;
				}
				printf("buzzer_fd ;%d.\n",buzzer_fd);	
				if(cmd_buzzer == 0x51){
					ioctl(buzzer_fd,BEEP_ON);
#if 1
					for(i = 0;i < sizeof(MUSIC)/sizeof(MUSIC[0]); i += 2)
					{
						beeper.tcnt = MUSIC[i];
						beeper.tcmp = MUSIC[i]/2;
						ioctl(buzzer_fd,BEEP_FREQ,&beeper);
						usleep(MUSIC[i+1] * 20000);
					} //��ʱ���������������̵߳Ĵ��������б����ʱ����
					printf("music play over....\n");
					close(buzzer_fd);
#endif 
				}else if(cmd_buzzer == 0x50){
					ioctl(buzzer_fd,BEEP_OFF);
				}else {
					printf("cmd_buzzer error.\n");
				}
				
				pthread_mutex_unlock(&mutex_buzzer);
				break;
			case 3L:  //数码管
				pthread_mutex_lock(&mutex_led);
				printf("hello seg\n");
				cmd_seg = msgbuf.text[0];
				pthread_mutex_unlock(&mutex_led);
				pthread_cond_signal(&cond_led);
				break;
			case 4L: //温湿度
				pthread_mutex_lock(&mutex_sqlite);
				printf("hello fan\n");
				cmd_fan = msgbuf.text[0];
				
				int fan_fd = open_port("/dev/ttyUSB0"); //���豸
				if(fan_fd < 0){
					printf("open failed\n");
				}
				set_com_config(fan_fd, 115200, 8, 'N', 1); //���ô��ڲ���
				
				unsigned char cmdbuf[2] = {0};
				if(cmd_fan == 0x23){
					strcpy(cmdbuf, "3\n"); //ע���
					write(fan_fd,&cmdbuf,sizeof(cmdbuf));
					printf("cmdbuf=%s\n", cmdbuf);
					sleep(2);
				}
				else if(cmd_fan == 0x22){
					strcpy(cmdbuf,"2\n"); //ע���
					write(fan_fd,&cmdbuf,sizeof(cmdbuf));
					printf("cmdbuf=%s\n", cmdbuf);
					sleep(2);
				}
				else if(cmd_fan == 0x21){
					strcpy(cmdbuf, "1\n"); //ע���
					write(fan_fd,&cmdbuf,sizeof(cmdbuf));
					printf("cmdbuf=%s\n", cmdbuf);
					sleep(2);
				}
				else if(cmd_fan == 0x20){
					strcpy(cmdbuf,"0\n");
					write(fan_fd,&cmdbuf,sizeof(cmdbuf));
					printf("cmdbuf=%s\n", cmdbuf);
					sleep(2);
					sleep(2);
				}
				else{
					printf("input error\n");
				}
				char buf[32] = {0};
				//get data from zigbee
				fcntl(fan_fd, F_SETFL, FNDELAY);
				read(fan_fd,&buf,sizeof(buf));
				printf("sizeof(buf) = %d.\n",sizeof(buf));
				printf(">>>>>>%s\n",buf);
				sleep(1);
				pthread_mutex_unlock(&mutex_sqlite);
				break;
			case 5L:
				printf("set env data\n");
				printf("temMAX: %d\n",*((int *)&msgbuf.text[1]));
				printf("temMIN: %d\n",*((int *)&msgbuf.text[5]));
				printf("humMAX: %d\n",*((int *)&msgbuf.text[9]));
				printf("humMAX: %d\n",*((int *)&msgbuf.text[13]));
				printf("illMAX: %d\n",*((int *)&msgbuf.text[17]));
				printf("illMAX: %d\n",*((int *)&msgbuf.text[21]));

				break;
			case 6L:
			case 7L:
			case 8L:
			case 9L:
				printf("����Խ���Щ��Ϊ��չ��ѧϰ������.\n");
				break;
			case 10L:
				{
					pthread_mutex_lock(&mutex_sms);
					int i = 0 , j = 0 ;
					for(i = 0 ; i < 11; i++){
						recive_phone[i] = msgbuf.text[i]; 	
					}
					recive_phone[i] = '\0';
					printf("recive:%s\n",recive_phone);
					for(j = 0 ;msgbuf.text[i] != '\0' && j < 12; i++, j++)
					{
						center_phone[j] =  msgbuf.text[i];
					}
					center_phone[j] = '\0';
					printf("center:%s\n",center_phone);
#if 0
					pthread_mutex_lock (&mutex_slinklist);
					sqlite_InsertLinknode (ENV_UPDATE, all_info_RT, sto_no, 0);//0,0分别是仓库号和货物种类号
					pthread_mutex_unlock (&mutex_slinklist);
					pthread_cond_signal (&cond_sqlite);
#endif 
					cmd_sms = msgbuf.text[0];
					pthread_cond_signal(&cond_sms);
					pthread_mutex_unlock(&mutex_sms);
				}
				break;
			default:
				break;

		}
	}

}


#if 0

long msgtype;//�������Ϣ����
��Ϣ���͵ķ��䣺
1L: 		LED����
2L:			����������
3L:			��·LED��ģ��������
4L:			����
5L:			��ʪ����ֵ����
6L-7L-8L-9L,���ڸ��˵���չ
10L: 		3Gͨ��ģ��-GPRS 
switch(msgbuf.msgtype){
	case 1L: ...  break;
			 ....
				 default ....  break;
}
#endif 





